package QuizPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

public class PictureResponse extends Question{
	public static String tableName = "pictureresponse";
	private String imageURL;
	private boolean casesensitive;
	private String[] responses;
	private String question;
	
	public PictureResponse(int questionID, String imageURL, String[] responses) {
		super(questionID);
		this.imageURL = imageURL;
		this.responses = responses;
	}
	
	public PictureResponse(int questionID, ResultSet rs) {
		super(questionID);
		try {
			casesensitive = Integer.parseInt(rs.getString("casesensitive")) == 1;
			imageURL = rs.getString("imageURL");
			question = rs.getString("question");
			//System.out.println("This is"+imageURL);
			StringTokenizer responseTokens = new StringTokenizer(rs.getString("response"), "|");
			responses = new String[responseTokens.countTokens()];
			int i=0;
			while (responseTokens.hasMoreTokens()) {
				responses[i] = responseTokens.nextToken();
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public String getHTML() {
		StringBuilder html = new StringBuilder();
		html.append(String.format("<img src=\"%s\"/><br/>", imageURL));
		html.append("<p>"+question+"</p>");
		html.append("<input name=\"" + questionID + "\" type=\"text\"> <br />\n");
		String result = new String(html);
		return result;
	}
	
	public int checkAnswer(String[] params) {
		for (int i=0; i<responses.length; i++) {
			if (!casesensitive && params[0].compareToIgnoreCase(responses[i]) == 0) return 1;
			if (casesensitive && params[0].equals(responses[i])) return 1;
		}
		
		return 0;
	}
	
	public int totalPoints() {
		return 1;
	}
	
	public String correctAnswer() {
		return responses[0];
	}
	
	public String toString() {
		return imageURL;
	}
	
	/**
	 * Returns a String of HTML that describes the UI users
	 * should use to create this question.  Returns only input
	 * types and text...does NOT return a form.
	 */
	public static String createQHTML()
	{
		StringBuilder html = new StringBuilder();
		html.append("<p>Enter the image url.</p>");
		html.append("<input name=\"imageURL\" type=\"text\">");
		html.append("<p>Enter the question.</p>");
		html.append("<input name=\"question\" type=\"text\" value=\"Name this picture?\">");
		html.append("<p>Enter the preferred response.</p>");
		html.append("<input name=\"preferred\" type=\"text\">");
		html.append("<p>Enter up to 4 additional responses.</p>");
		html.append("<input name=\"r1\" type=\"text\">");
		html.append("<input name=\"r2\" type=\"text\">");
		html.append("<input name=\"r3\" type=\"text\">");
		html.append("<input name=\"r4\" type=\"text\">");
		html.append("<p>Enter additional responses.(Separate responses with only a | character)");
		html.append("<input name=\"extra\" type=\"text\">");
		
		html.append("<p>Should this question be case-sensitive?</p>");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"false\" checked>No");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"true\">Yes");
		
		String result = new String(html);
		return result;
	}
	
	/**
	 * Enters the question with the form data from request
	 * into the proper question database table.  Will read
	 * the same input as created in createQHTML.
	 */
	public static void submitQuestion(int quizid,HttpServletRequest request)
	{
		QuizDBC dbc = new QuizDBC();
		String image = request.getParameter("imageURL");
		StringBuilder response = new StringBuilder();
		response.append(request.getParameter("preferred"));
		String question = request.getParameter("question");
		int casesensitive = (request.getParameter("casesensitive").equals("false")) ? 0 : 1;
		for(int i=1 ; i <= 4 ; i++)
		{
			if(request.getParameter("r"+i).length()>0) response.append("|"+request.getParameter("r"+i));
		}
		if(request.getParameter("extra").length()>0) response.append("|"+request.getParameter("extra"));
		dbc.editTable("INSERT INTO pictureresponse VALUES(\""+image+"\",\""+response+"\","+quizid+",\""+question+"\","+casesensitive+");");
	}
}
